$(function () {
    getMsg();
});

function getMsg() {
    var params = JSON.stringify({"conventionerId": $.cookie("user")});
    $.ajax({
        // url:'http://125.65.40.171/conference-check/conferenceController/selectBranchAddressAllowSign',
        url: 'conferenceController/selectBranchAddressAllowSign',
        dataType: 'json',
        data: {"param": params},
        type: "post",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function (data) {
            if (data.code == 200) {
                $.each(data.data, function (e, v) {
                    $(".collect").append("<optgroup label='" + v.name + "'></optgroup>");
                    $.each(v.branchList, function (e, v) {
                        // var starTime = new Date(v.startDate).Format("yyyy/MM/dd HH:mm");
                        // var endTime = new Date(v.endDate).Format("yyyy/MM/dd HH:mm");
                        // starTime = format(starTime + "");
                        // endTime = format(endTime + "");
                        var date = v.date;
                        $(".collect").find("optgroup:last").append("<option conferenceId='" + v.conferenceId + "' value='" + v.id + "'>" +
                            "<p>" + date + "</p>" +
                            "<p>" + v.site + "</p>" +
                            "</option>");
                    });
                });
                $(".collect").val("");
            } else if (data.code == 500) {
                myApp.alert('', '没有可签到会议');
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            myApp.alert('', '系统错误，请稍后重试');
        }
    })
}

$(".submit").on("click", function () {
    var branchId = $(".collect").val();
    if (branchId == null) {
        myApp.alert('', '请选择会场');
    } else {
        var params = JSON.stringify({"status": "1", "recordId": branchId});
        $.ajax({
            // url:'http://125.65.40.171/conference-check/attendRecordController/editBranchAddressStatus',
            url: 'attendRecordController/editBranchAddressStatus',
            dataType: 'json',
            data: {"param": params},
            type: "post",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                if (data.code == 200) {
                    window.location.href = "myApply";
                } else if (data.code == 500) {
                    myApp.alert('', '签到失败');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                myApp.alert('', '系统错误，请稍后重试');
            }
        })
    }
});

function format(str) {
    if (str != undefined && str != "" && str != null) {
        if (str.toString().indexOf('T') != -1) {
            //str = new Date(str).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, ''); ///去掉日期中的T
            str = str.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, ''); ///去掉日期中的T
        }
        str = str.replace(/-/g, "/"); //将-替换为/，因为ios中不支持-和T

        var formatDate = new Date(Date.parse(str));

        var yy = formatDate.getFullYear();
        var MM = formatDate.getMonth() + 1;
        if (MM < 10) {
            MM = '0' + MM;
        }
        var dd = formatDate.getDate();
        if (dd < 10) {
            dd = '0' + dd;
        }
        var setDate = yy + "-" + MM + "-" + dd;

        return setDate;
    } else {
        str = "";
    }
};